package com.reservationserver.controller.admin;

import com.reservationpojo.domin.po.Mgrlogpo;
import com.reservationpojo.domin.vo.Addmgrvo;
import com.reservationpojo.domin.vo.Mgrlogvo;
import com.reservationpojo.entity.Mgr;
import com.reservationpojo.utils.Result;
import com.reservationserver.mapper.admin.Mgrmapper;
import com.reservationserver.service.admin.Mgrservice;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;


@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/reservation/mgrlogin")
@Tag(name = "管理员登录")
public class Mgrlogincontroller {
    private final Mgrservice mgrservice;
    private final Mgrmapper mgrmapper;
    private final RedisTemplate<String, String> redisTemplate;

    /**
     *
     * @param mgrlog
     * @return
     * \/
     */
    @Operation(summary = "登录")
    @GetMapping("/login")
    public Result mgrlogin(@RequestBody Mgrlogvo mgrlog){
        log.info("{}",mgrlog);
        Mgrlogpo mgrlogpo = new Mgrlogpo();
        Mgr mgr= mgrmapper.mgrlogin(mgrlog);
        log.info("mgr:{}",mgr);
        if(mgr!=null){
            String jwt=mgrservice.mgrlogin(mgr);
            mgrlogpo.setJwt(jwt);
//            mgrlogpo.setInitial(mgr.isInitial());
            redisTemplate.opsForValue().set(mgrlog.getMgrId(),jwt,1, TimeUnit.DAYS);
            return Result.success(mgrlogpo);
        }

        return Result.error("账号或密码错误");


    }


}
